import axios from 'axios'
import { Message, MessageBox } from 'element-ui'
import store from '@/store'
import router from '../router'
// import { getToken } from '@/utils/auth'
import Promise from 'promise-polyfill'

// To add to window  解决promise 在ie中未定义的问题
if (!window.Promise) {
  window.Promise = Promise
}
axios.defaults.withCredentials = true
// create an axios instance
const service = axios.create({
  baseURL: process.env.BASE_API, // api 的 base_url
  timeout: 5000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // Do something before request is sent 在发出请求前做点什么
    if (store.getters.token) {
      // config.headers['X-Litemall-Admin-Token'] = getToken() 配置头['X-Litemall-Admin-Token']=getToken（）
    }
    return config
  },
  error => {
    // Do something with request error 处理请求错误
    console.log(error) // for debug
    Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    const res = response.data

    if (res.code === '401') {
      MessageBox.alert('参数错误', '错误', {
        confirmButtonText: '确定',
        type: 'error'
      }).then(() => {
        store.dispatch('FedLogOut').then(() => {
          location.reload()
        })
      })
      return Promise.reject('error')
    } else if (res.code === '304') {
      MessageBox.alert('查无此帐号', '错误', {
        confirmButtonText: '确定',
        type: 'error'
      }).then(() => {
        store.dispatch('FedLogOut').then(() => {
          location.reload()
        })
      })
      return Promise.reject('error')
    } else if (res.code === '404' || res.code === '405') {
      MessageBox.alert(res.message, '错误', {
        confirmButtonText: '确定',
        type: 'error'
      }).then(() => {
        store.dispatch('FedLogOut').then(() => {
          location.reload()
        })
      })
      return Promise.reject('error')
    } else if (res.code === '403') {
      // MessageBox.alert('未登录', '错误', {
      //   confirmButtonText: '确定',
      //   type: 'error'
      // })
      return Promise.reject('error')
    } else if (res.code === '307') {
      return Promise.reject(res)
    } else if (res.code === '308') {
      return Promise.reject(res)
    } else if (res.code === '309') {
      MessageBox.alert(res.message, '错误', {
        confirmButtonText: '确定',
        type: 'error'
      }).then(() => {
        store.dispatch('FedLogOut').then(() => {
          location.reload()
        })
      })
      return Promise.reject('error')
    } else if (res.code === '311') {
      return Promise.reject(res)
    } else if (res.code === '314') {
      MessageBox.alert(res.message, '错误', {
        confirmButtonText: '确定',
        type: 'error'
      }).then(() => {
        router.push({
          path: '/'
        })
      })
      return Promise.reject('error')
    } else {
      return response
    }
  }, error => {
    console.log('err' + error)// for debug
    Message({
      message: '登录连接超时（后台不能连接，请联系系统管理员）',
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  })

export default service
